<!--
Copyright © 2017, Che-Wei Hsu <cwxhsu@gmail.com>
This file is part of the MintCM.
Some rights reserved. See README.
-->

<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="../css/mcm_style.css">
</head>

<body class="css_body">

<div class="css_div_box_frame_full">
  <div class="css_div_box_title">chapter 05-01</div>
  <div class="css_div_box_content">
  </div>
</div>
<br>

<div class="css_div_box_frame_full">
  <div class="css_div_box_title">外部程式和內部模組的資料傳遞</div>
  <div class="css_div_box_content">
    <br>

    外部程式在使用 <a href="mcm_0302.html#hook_0302_mcm_lulib_run_01">mcm_lulib_run</a> /
    <a href="mcm_0303.html#hook_0303_mcm_lklib_run_01">mcm_lklib_run</a> 執行內部模組時,
    可以傳遞資料給內部模組, 而內部模組也可以回傳資料給外部程式.
    <br><br>

  </div>
</div>
<br>

<div class="css_div_box_frame_full">
  <div class="css_div_box_title">變數格式的說明</div>
  <div class="css_div_box_content">
    <br>

    <font class="css_font_b1">[struct mcm_service_session_t *this_session]</font><br>
    請求端的連線資訊. <a href="mcm_0402.html#hook_0402_this_session_01">[詳細]</a><br>
    會使用到的結構成員 :<br>
    <table class="css_table_list2">
      <tr>
        <td class="css_td_list2_l1">
          <font class="css_font_r1">void *</font><br>
          <font class="css_font_g1">req_data_con</font>
        </td>
        <td class="css_td_list2_r1">
          外部程式傳來的資料 <a href="mcm_0302.html#hook_0302_req_data_con_01">[來源-User]</a>
          <a href="mcm_0303.html#hook_0303_req_data_con_01">[來源-Kernel]</a><br>
          <font class="css_font_p1">NULL 表示外部程式沒有傳送資料給模組函式</font>
        </td>
      </tr>
      <tr>
        <td class="css_td_list2_l1">
          <font class="css_font_r1">MCM_DTYPE_USIZE_TD</font><br>
          <font class="css_font_g1">req_data_len</font>
        </td>
        <td class="css_td_list2_r1">
          外部程式傳來的資料的長度 <a href="mcm_0302.html#hook_0302_req_data_len_01">[來源-User]</a>
          <a href="mcm_0303.html#hook_0303_req_data_len_01">[來源-Kernel]</a><br>
          <font class="css_font_p1">如果 req_data_con 是 NULL 則為 0</font>
        </td>
      </tr>
      <tr>
        <td class="css_td_list2_l1">
          <div class="css_div_hook" id="hook_0501_rep_data_buf_01"></div>
          <font class="css_font_r1">void *</font><br>
          <font class="css_font_g1">rep_data_buf</font>
        </td>
        <td class="css_td_list2_r1">
          紀錄內部模組要回傳的資料 <a href="mcm_0302.html#hook_0302_rep_data_buf_01">[目的-User]</a>
          <a href="mcm_0303.html#hook_0303_rep_data_buf_01">[目的-Kernel]</a><br>
          <font class="css_font_p1">預設值是 NULL, 表示內部模組沒有要回傳資料</font><br>
          <font class="css_font_r1">
          要回傳資料的話<font class="css_font_r2">必須動態配置一塊記憶體</font>,
          並將得到的位址給 rep_data_buf, 後續程式會自動釋放配置的記憶體
          </font>
        </td>
      </tr>
      <tr>
        <td class="css_td_list2_l1">
          <font class="css_font_r1">MCM_DTYPE_USIZE_TD</font><br>
          <font class="css_font_g1">rep_data_len</font>
        </td>
        <td class="css_td_list2_r1">
          紀錄內部模組要回傳的資料的長度 <a href="mcm_0302.html#hook_0302_rep_data_len_buf_01">[目的-User]</a>
          <a href="mcm_0303.html#hook_0303_rep_data_len_buf_01">[目的-Kernel]</a><br>
          <font class="css_font_p1">
          預設值是 0, 表示內部模組沒有要回傳資料, rep_data_len 的值可以小於 rep_data_buf
          配置的記憶體大小, 實際回傳的資料長度以 rep_data_len 為準
          </font>
        </td>
      </tr>
    </table>
    <br>
    範例 :<br>
<pre class="css_pre_code">
int mcm_module_data_test(
    struct mcm_service_session_t *this_session)
{
    MCM_DTYPE_USIZE_TD tmp_len;
    char *tmp_buf;

    // 外部程式傳來的資料.
    printf("req [" MCM_DTYPE_USIZE_PF "][%p]",
           this_session->req_data_len, this_session->req_data_con);

    // 內部模組要回傳的資料.

    tmp_len = 128;
    tmp_buf = (char *) malloc(tmp_len);

    snprintf(tmp_buf, tmp_len, "internal data");

    this_session->rep_data_buf = tmp_buf;
    this_session->rep_data_len = strlen(tmp_buf) + 1;

    return MCM_RCODE_PASS;
}
</pre>
<br><br>

  </div>
</div>
<br>

</body>

</html>
